﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.Mvc;
using System.Reflection;
using System.Diagnostics;
using QGZX.Helper;
using MODEL.ViewModel;
using MODEL;
 


namespace QGZX.Logic.Zmjh
{
    public class ExcelController :Controller
    {

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Upload(Course model)
        {
            ExcelHelper helper = new ExcelHelper();
            ///获得文件列表中excel的每一个对象的第一个sheet并转换成datatable
            //List<DataTable> dtList = helper.SaveExcelAndReadExcel(Request);
            //导入excel--只接受excel2003-2007格式
            List<Course> listModel = helper.SaveExcelAndReadExcel<Course>(Request);

            ///**********在这里编写保存泛型对象列表到数据库的语句，并在界面上提示导入成功************
            return View(model);
        }


        //#region ------------------Excel暂时只能导出表头的数据------------备份---------------
        ///// <summary>
        ///// 文件下载
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public ActionResult Download(qgzx_zm_course model)
        //{
        //    //String CourseName = Request.QueryString["CourseName"];
        //    String CourseName = "心理健康";
        //    ExcelHelper helper = new ExcelHelper();
        //    //**************************此段为测试代码**********************************
        //    DataTable dt = new DataTable();
        //    //PBCommendPlanManager manager = new PBCommendPlanManager();

        //    //qgzx_zm_course userPar = new qgzx_zm_course(); //
        //    //userPar.CourseName = CourseName;
        //    //List<Course> userParList = manager.GetAllCommendPlans(userPar);

        //    List<qgzx_zm_course> list = OperateContext.Current.BLLSession.Iqgzx_zm_courseBLL.GetListBy(x => x.CourseName == CourseName, m => m.Id);
        //    DataColumn col = new DataColumn("ID");//导出excel表头
        //    DataColumn col1 = new DataColumn("课程名称");
        //    DataColumn col2 = new DataColumn("课程大纲");
        //    DataColumn col3 = new DataColumn("课程目的");
        //    DataColumn col4 = new DataColumn("课程介绍");
        //    DataColumn col5 = new DataColumn("是否开启");

        //    dt.Columns.Add(col);
        //    dt.Columns.Add(col1);
        //    dt.Columns.Add(col2);
        //    dt.Columns.Add(col3);
        //    dt.Columns.Add(col4);
        //    dt.Columns.Add(col5);

        //    foreach (var item in list)
        //    {
        //        DataRow dr = dt.NewRow();
        //        dr[0] = item.Id.ToString();
        //        dr[1] = item.CourseName.ToString();
        //        dr[2] = item.CourseSubject.ToString();
        //        dr[3] = item.CoursePurpose.ToString();
        //        dr[4] = item.CourseIntroduce.ToString();
        //        dr[5] = item.IsOpen.ToString();
        //        dt.Rows.Add(dr);
        //    }
        //    //********************************************************************
        //    //导出excel--默认excel2003-2007
        //    helper.ExportExcel(dt, Response);
        //    return new EmptyResult();
        //} 
        //#endregion

        #region ------------------Excel暂时只能导出表头的数据-----------修改测试----------------
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Download(qgzx_zm_course model)
        {
            //String CourseName = Request.QueryString["CourseName"];
            String CourseName = "心理健康";
            ExcelHelper helper = new ExcelHelper();
            //**************************此段为测试代码**********************************
            DataTable dt = new DataTable();
          
            var list = OperateContext.Current.BLLSession.Iqgzx_zm_courseBLL.GetListBy(x => x.CourseName == CourseName, m => m.Id);
            //foreach循环list集合，每一个list再根据id查询另外一张表的数据，把需要的字段追加进来

            DataColumn col = new DataColumn("ID");//导出excel表头
            DataColumn col1 = new DataColumn("课程名称");
            DataColumn col2 = new DataColumn("课程大纲");
            DataColumn col3 = new DataColumn("课程目的");
            DataColumn col4 = new DataColumn("课程介绍");
            DataColumn col5 = new DataColumn("是否开启");

            dt.Columns.Add(col);
            dt.Columns.Add(col1);
            dt.Columns.Add(col2);
            dt.Columns.Add(col3);
            dt.Columns.Add(col4);
            dt.Columns.Add(col5);

            foreach (qgzx_zm_course item in list)
            {
                DataRow dr = dt.NewRow();
                dr[0] = item.Id.ToString();
                dr[1] = item.CourseName.ToString();
                dr[2] = item.CourseSubject.ToString();
                dr[3] = item.CoursePurpose.ToString();
                dr[4] = item.CourseIntroduce.ToString();
                dr[5] = item.IsOpen.ToString();
                dt.Rows.Add(dr);
            }
            //********************************************************************
            //导出excel--默认excel2003-2007
            helper.ExportExcel(dt, Response);
            return new EmptyResult();
        }
        #endregion

}
 
 



    }
   

